<?php
class CitiesController extends AppController {

	var $name = 'Cities';
	var $helpers = array('Html', 'Form');

	function beenHere($city_id)
	{
		$user = $this->Auth->user();
		$user_id = $user['User']['id'];
		
		$profile = $this->City->Profile->findByUserId($user_id);
		
		
		$this->data = array('Visited' => array('profile_id' => $profile['Profile']['id'], 'city_id' => $city_id));
		$this->City->Visited->Create();
		$this->City->Visited->save($this->data);
	
		
	
		$this->redirect(array('controller' => 'cities', 'action' => 'view', $city_id));
	}
	
	function siteAutoComplete($user_limit = 4, $city_limit = 8)
	{
		$redirect = $this->Auth->redirect();
		if(strpos($redirect,'/siteAutoComplete/') !== FALSE)	
			$this->Auth->redirect( '/profiles/view' );
		 
		$query = $this->data['query'];	
		
			
		$this->City->unbindModel(array('hasMany' => array('Profile','Story','Post')));
		
		$result = $this->City->find('all', array(
			 'conditions' => array(
			 	'City.ascii_name LIKE' =>  $query . '%'
			 	),
			 'recursive' => 1,
			 'order' => array('City.population DESC'),
			 'limit' => $city_limit
			 ));
			 
		//$this->log("City Search\n:" . serialize($result) . "\n\n");
		

		/* allow for more user finds if we have less than $city_limit results */
		$user_limit += ($city_limit - count($result));
		
		$this->set('cities', $result);
		
		$users = $this->requestAction("/users/findUsers/" . $query . '/' . $user_limit);
		$this->set('users', $users);
		//$this->log("User Search\n:" . serialize($users) . "\n\n");
		
		$this->layout = 'ajax';
		
	}
	
	
	function autoComplete($limit = 10, $query = null)
	{	 
		if($query != null)
		 $this->data['City']['ascii_name'] = $query;
		 
		 
		$this->City->unbindModel(array('hasMany' => array('Profile','Story','Post')));
		
		$result = $this->City->find('all', array(
			 'conditions' => array(
			 	'City.ascii_name LIKE' => '%' . $this->data['City']['ascii_name'] . '%'
			 	),
			 'recursive' => 1,
			 'order' => array('City.population DESC'),
			 'limit' => $limit
			 ));
		
		
		//$this->log(serialize($result),'activity');
		$this->set('cities', $result);
		$this->layout = 'ajax';
	}
	

	function view($id = null) {
		if (!$id) {
			$this->Session->setFlash(__('Invalid city.', true));
			$this->redirect(array('action'=>'index'));
		}
		
		$user = $this->Auth->user();
		$user_id = $this->City->Profile->findByUserId($user['User']['id']);
		$usercityid = $user_id['Profile']['city_id'];
		$this->set('usercityid', $usercityid);
		
		$notbeenhere = $this->City->Visited->find('all',
			array('conditions' =>  
    						array('profile_id' => $user_id['Profile']['id'], 'city_id' => $id)
    							, 'limit' => 20, 'order' => 'RAND()'));
		
    	$this->set('notbeenhere', empty($notbeenhere));
		
		$this->City->Post->recursive = 3;
		$this->City->Post->unbindModel(array('belongsTo' => array('City','Profile')));
		$this->City->Post->User->unbindModel(array('hasMany' => array('Story')));
		$this->City->Post->User->Profile->unbindModel(array('hasMany' => array('Post'), 'belongsTo' => array('User')));
		$this->City->Post->User->unbindModel(array('hasAndBelongsToMany' => array('Friend')));
		$posts = $this->City->Post->find('all', array('conditions' => array('Post.city_id' => $id), 'order' => 'Post.created DESC'));
		
		$this->set('posts',$posts);
		
		$this->City->recursive = 2;
		
		$this->City->unbindModel(array('hasMany' => array('Post')));
		$this->City->Country->unbindModel(array('hasMany' => array('Region','City')));
		$this->City->Region->unbindModel(array('hasMany' => array('City')));
		
		$city = $this->City->findById($id);
		array_splice($city['Profile'],20);
		shuffle($city['Profile']);
		
		if(!isset($city['City']['id']))
		 $this->cakeError('error404');
		
		//debug($city);
		$this->set('city', $city);
	}

	function beforeFilter() {
		$this->Session->write('Auth.redirect', $this->here);
		$this->Auth->allow('index', 'view', 'autoComplete', 'siteAutoComplete', 'site_auto_complete', 'auto_complete');
	}
}
?>